package com.fengchao.api.controller.v1;

import com.alibaba.fastjson.JSON;
import com.fengchao.api.config.Constant;
import com.fengchao.api.service.CfgService;
import com.fengchao.model.cfg.CfgApiConfig;
import com.fengchao.model.cfg.CfgAppConfig;
import com.fengchao.model.cfg.CfgRegion;
import com.fengchao.model.cfg.CfgSongType;
import com.fengchao.model.entity.comm.CustomBizException;
import com.fengchao.model.entity.comm.Message;
import com.fengchao.model.entity.comm.ResponseParameter;
import com.fengchao.util.common.CommonValidator;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.fengchao.api.config.Constant.GET_SUCCESS;
import static com.fengchao.api.config.Constant.PARAMETER_IS_NOT_COMPLETE;

/**
 * 订单
 * Created by 王念 on 2016/6/20.
 */
@RequestMapping("/v1/cfg")
@RestController
public class CfgController {


    @Autowired
    private CfgService cfgService;

    private static Logger logger = LoggerFactory.getLogger(CfgController.class);

    /**
     * 获取城市
     *
     * @return
     */
    @ApiOperation(value = "(APP/WEB)获取城市", notes = "返回参数:</br>成功:</br>{status: SUCCESS, </br>message: 获取成功,</br>data:{</br>&nbsp;&nbsp;info:[</br>&nbsp;&nbsp;&nbsp;{</br>&nbsp;&nbsp;&nbsp;&nbsp;id: 1(地区ID),</br>&nbsp;&nbsp;&nbsp;&nbsp;pid: 0(父类id id的外键),</br>&nbsp;&nbsp;&nbsp;&nbsp;code: 110000(code)," +
            "</br>&nbsp;&nbsp;&nbsp;&nbsp;name: 北京(),</br>&nbsp;&nbsp;&nbsp;&nbsp;level: 2(级别，2：省（直辖市），3：市，4：区),</br>&nbsp;&nbsp;&nbsp;&nbsp;cityCode: aaa(城市Code),</br>&nbsp;&nbsp;&nbsp;&nbsp;oderId: null(排序ID)</br>&nbsp;&nbsp;&nbsp;}</br>&nbsp;&nbsp;]</br>&nbsp;}</br>}")
    @RequestMapping(value = "/getCfgRegion", method = RequestMethod.GET)
    public ResponseParameter getCfgRegion(@ApiParam(value = "pid,pid为0获取所有的省") @RequestHeader(required = false) Integer pid) {
        ResponseParameter responseParameter = new ResponseParameter(Message.SUCCESS, GET_SUCCESS);
        List<CfgRegion> list = cfgService.getCfgRegionByPid(pid);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("info", list);
        responseParameter.setData(map);
        return responseParameter;
    }

    /**
     * 获取曲风
     *
     * @return
     */
    @ApiOperation(value = "获取所有曲风", notes = "返回参数:</br>成功:</br>{status: SUCCESS, </br>message: 获取成功,</br>data:{</br>&nbsp;&nbsp;info:[</br>&nbsp;&nbsp;&nbsp;{</br>&nbsp;&nbsp;&nbsp;&nbsp;id: 1(类型ID),</br>&nbsp;&nbsp;&nbsp;&nbsp;typeName: 流行(类型名称)" +
            "</br>&nbsp;&nbsp;&nbsp;}</br>&nbsp;&nbsp;]</br>&nbsp;}</br>}")
    @RequestMapping(value = "/getCfgSongType", method = RequestMethod.GET)
    public ResponseParameter getCfgSongType() {
        ResponseParameter responseParameter = new ResponseParameter(Message.SUCCESS, GET_SUCCESS);
        List<CfgSongType> list = cfgService.getCfgSongTypeALL();
        HashMap<Object, Object> map = new HashMap<>();
        map.put("info", list);
        responseParameter.setData(map);
        return responseParameter;
    }

    /**
     * 获取省市区JSON
     *
     * @return
     */
    @ApiOperation(value = "(APP/WEB)获取省市区JSON", notes = "返回参数:</br>[</br>&nbsp;{</br>&nbsp;&nbsp;provinceName: 北京(省会名称),</br>&nbsp;&nbsp;&nbsp;provinceId: 1(省会ID),</br>&nbsp;&nbsp;cityList:[</br>&nbsp;&nbsp;&nbsp;{</br>&nbsp;&nbsp;&nbsp;&nbsp;districtList:[</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{" +
            "</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;districtId: 2(区域ID),</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;districtName: 东城区(区域名称),</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</br>&nbsp;&nbsp;&nbsp;&nbsp;]</br>&nbsp;&nbsp;&nbsp;}</br>&nbsp;&nbsp;]</br>&nbsp;}</br>]")
    @RequestMapping(value = "/getCfgRegionToJson", method = RequestMethod.GET)
    public List<Map<String, Object>> getCfgRegionToJson() {
        List<Map<String, Object>> resultList1 = new ArrayList<>();
        List<CfgRegion> list1 = cfgService.getCfgRegionByPid(0);
        for (CfgRegion cfgRegion1 : list1) {
            Map<String, Object> map1 = new HashMap<>();
            map1.put("provinceId", cfgRegion1.getId());
            map1.put("provinceName", cfgRegion1.getName());

            List<Map<String, Object>> resultList2 = new ArrayList<>();
            List<CfgRegion> list2 = cfgService.getCfgRegionByPid(cfgRegion1.getId());
            for (CfgRegion cfgRegion2 : list2) {
                Map<String, Object> map2 = new HashMap<>();
                map2.put("cityId", cfgRegion2.getId());
                map2.put("cityName", cfgRegion2.getName());

                List<Map<String, Object>> resultList3 = new ArrayList<>();
                List<CfgRegion> list3 = cfgService.getCfgRegionByPid(cfgRegion2.getId());
                for (CfgRegion cfgRegion3 : list3) {
                    Map<String, Object> map3 = new HashMap<>();
                    map3.put("districtId", cfgRegion3.getId());
                    map3.put("districtName", cfgRegion3.getName());
                    resultList3.add(map3);
                }
                map2.put("districtList", resultList3);
                resultList2.add(map2);
            }
            map1.put("cityList", resultList2);
            resultList1.add(map1);
        }
        return resultList1;
    }

    /**
     * 获取webBanner
     *
     * @return
     */
    @ApiOperation(value = "(WEB)获取Banner", notes = "返回参数: </br>成功:</br>{status: SUCCESS, </br>message: 获取成功,</br>data:{</br>&nbsp;&nbsp;info[</br>&nbsp;&nbsp;&nbsp;{</br>&nbsp;&nbsp;&nbsp;&nbsp;foreignId: 1(外键ID),</br>&nbsp;&nbsp;&nbsp;&nbsp;webImage: http://aaa.png (web的banner图片),</br>&nbsp;&nbsp;&nbsp;&nbsp;buttonText: aaa(pc的按钮名称)," +
            "</br>&nbsp;&nbsp;&nbsp;&nbsp;title: 音乐会(banner标题),</br>&nbsp;&nbsp;&nbsp;&nbsp;type: ARTIST(banner类型 HOME(主页)CROWDFUNDING(众筹)ARTIST(艺人))</br>&nbsp;&nbsp;&nbsp;}</br>&nbsp;&nbsp;]</br>&nbsp;}</br>}" +
            "</br>失败: </br>{status: FAILURE, message: 参数不全}")
    @RequestMapping(value = "/getCfgWebBanner/{bannerType}", method = RequestMethod.GET)
    public ResponseParameter getCfgWebBanner(@ApiParam(value = "(必填)类型,WEB", required = true) @RequestHeader String type,
                                             @ApiParam(value = "(必填)banner类型 HOME(主页)CROWDFUNDING(众筹)ARTIST(艺人) ", required = true) @PathVariable String bannerType) {
        ResponseParameter responseParameter = new ResponseParameter(Message.SUCCESS, GET_SUCCESS);
        logger.info("获取Banner(web）请求参数= type:{},bannerType:{}", type, bannerType);
        if (!CommonValidator.isStringsNotNull(type)) {
            throw new CustomBizException(PARAMETER_IS_NOT_COMPLETE);
        }
        List<Map<String, Object>> list = cfgService.getCfgWebBanner(bannerType);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("info", list);
        responseParameter.setData(map);
        logger.info("获取Banner(web）请求参数= {}", JSON.toJSON(responseParameter));
        return responseParameter;
    }

    /**
     * 获取webBanner
     *
     * @return
     */
    @ApiOperation(value = "(APP)获取Banner", notes = "返回参数: </br>成功:</br>{status: SUCCESS, </br>message: 获取成功,</br>data:{</br>&nbsp;&nbsp;info[</br>&nbsp;&nbsp;&nbsp;{</br>&nbsp;&nbsp;&nbsp;&nbsp;foreignId: 1(外键ID),</br>&nbsp;&nbsp;&nbsp;&nbsp;appImage: http://aaa.png (app的banner图片),</br>&nbsp;&nbsp;&nbsp;&nbsp;linkUrl: http://aaa.com (跳转链接，当Type=LINK时,使用本字段作为跳转网页的字段)," +
            "</br>&nbsp;&nbsp;&nbsp;&nbsp;title: 音乐会(banner标题),</br>&nbsp;&nbsp;&nbsp;&nbsp;type: NOFORWARD(类型 NOFORWARD(不跳转)SONGS(歌曲)AIBUM(专辑)CROWDFUNDING(众筹)ARTIST(艺人)LINK(超链接))</br>&nbsp;&nbsp;&nbsp;}</br>&nbsp;&nbsp;]</br>&nbsp;}</br>}" +
            "</br>失败: </br>{status: FAILURE, message: 参数不全}")
    @RequestMapping(value = "/getCfgAppBanner", method = RequestMethod.GET)
    public ResponseParameter getCfgAppBanner(@ApiParam(value = "(必填)类型, APP", required = true) @RequestHeader String type) {
        ResponseParameter responseParameter = new ResponseParameter(Message.SUCCESS, GET_SUCCESS);
        logger.info("获取Banner(app)请求参数= type:{}", type);
        if (!CommonValidator.isStringsNotNull(type)) {
            throw new CustomBizException(PARAMETER_IS_NOT_COMPLETE);
        }
        List<Map<String, Object>> list = cfgService.getCfgAppBanner();
        HashMap<Object, Object> map = new HashMap<>();
        map.put("info", list);
        responseParameter.setData(map);
        logger.info("获取Banner(app）响应参数= {}", JSON.toJSON(responseParameter));
        return responseParameter;
    }

    /**
     * 获取APP最新的版本信息
     *
     * @return
     */
    @Deprecated  //等强制更新稳定以后去掉该方法
    @ApiOperation(value = "(APP)获取最新的版本", notes = "返回参数: </br>成功: </br>{status: SUCCESS, </br>message: 获取成功,</br>data:{</br>&nbsp;&nbsp;info{</br>&nbsp;&nbsp;&nbsp;id: 1(ID),</br>&nbsp;&nbsp;&nbsp;terminalType: ANDROID(终端类型 ANDROID IOS),</br>&nbsp;&nbsp;&nbsp;lastVersion: 1.1.0(最新的版本号),</br>&nbsp;&nbsp;&nbsp;id: 1()," +
            "</br>&nbsp;&nbsp;&nbsp;buildId: 12(最新的buildID),</br>&nbsp;&nbsp;&nbsp;forceUpdateBuildId: 1(强制更新build-id),</br>&nbsp;&nbsp;&nbsp;updateContent: 修复了一大堆BUG(更新内容),</br>&nbsp;&nbsp;&nbsp;autoUpdateUrl: http://swarm-dev.b0.upaiyun.com/app2.apk(安卓的apk下载链接)</br>&nbsp;&nbsp;}</br>&nbsp;}</br>}")
    @RequestMapping(value = "/getLatestVersions", method = RequestMethod.GET)
    public ResponseParameter getLatestVersions(@ApiParam(value = "(必填)类型,APP", required = true) @RequestHeader String type,
                                               @ApiParam(value = "(必填)终端类型 (ANDROID,IOS)", required = true) @RequestHeader String terminalType) {
        ResponseParameter responseParameter = new ResponseParameter(Message.SUCCESS, GET_SUCCESS);
        logger.info("获取最新的版本(app)请求参数= type:{},terminalType:{}", type, terminalType);
        if (!CommonValidator.isStringsNotNull(type, terminalType)) {
            throw new CustomBizException(PARAMETER_IS_NOT_COMPLETE);
        }
        CfgAppConfig cfgAppConfig = cfgService.findCfgAppConfig(terminalType);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("info", cfgAppConfig);
        responseParameter.setData(map);
        logger.info("获取最新的版本(app）响应参数= {}", JSON.toJSON(responseParameter));
        return responseParameter;
    }

    /**
     * 获取申请众筹的属性配置
     *
     * @return
     */
    @RequestMapping(value = "/getCfgApplyCrowdfundingItemsList", method = RequestMethod.GET)
    @ApiOperation(value = "(WEB)获取申请众筹的属性配置", notes = "返回参数:</br>成功:</br>{status: SUCCESS, </br>message: 获取成功,</br>data:{</br>&nbsp;&nbsp;info:{</br>&nbsp;&nbsp;&nbsp;target_city:[</br>&nbsp;&nbsp;&nbsp;&nbsp;{</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item_display_value_cn: 北京(目的城市展示中文),</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item_display_value_en: Beijing(目的城市展示应文)," +
            "</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item_name: target_city(属性名),</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id: 1(唯一标识ID),</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item_option: option1(属性选项名)</br>&nbsp;&nbsp;&nbsp;&nbsp;}</br>&nbsp;&nbsp;&nbsp;]</br>&nbsp;&nbsp;}</br>&nbsp;}</br>}" +
            "</br>失败:</br>{status: FAILURE, message: 参数不全}")
    public ResponseParameter getCfgApplyCrowdfundingItemsList(@ApiParam(value = "(必填)类型,WEB", required = true) @RequestHeader String type) {
        ResponseParameter responseParameter = new ResponseParameter(Message.SUCCESS, GET_SUCCESS);
        logger.info("获取申请众筹的属性配置请求参数= type:{}", type);
        if (!CommonValidator.isStringsNotNull(type)) {
            throw new CustomBizException(PARAMETER_IS_NOT_COMPLETE);
        }
        Map<String, List<Map>> list = cfgService.getCfgApplyCrowdfundingItemsList();
        HashMap<Object, Object> map = new HashMap<>();
        map.put("info", list);
        responseParameter.setData(map);
        logger.debug("获取申请众筹的属性配置响应参数={}", JSON.toJSON(responseParameter));
        logger.info("获取申请众筹的属性配置响应参数,获取的数量{}", list.size());
        return responseParameter;
    }

    /**
     * 获取城市列表最后一次更新时间
     *
     * @return
     */
    @RequestMapping(value = "/getCityListUpdateDate", method = RequestMethod.GET)
    @ApiOperation(value = "(APP1.4版)获取城市列表最后一次更新时间", notes = "")
    public ResponseParameter getCityListUpdateDate(@ApiParam(value = "(必填)类型,APP", required = true) @RequestHeader String type) {
        ResponseParameter responseParameter = new ResponseParameter(Message.SUCCESS, GET_SUCCESS);
        logger.info("获取城市列表最后一次更新时间请求参数= type:{}", type);
        CfgApiConfig cfgApiConfig = cfgService.findCfgApiConfigByKey(Constant.cityListUpdateDate);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("date", Long.parseLong(cfgApiConfig.getValue()));
        responseParameter.setData(map);
        logger.info("获取城市列表最后一次更新时间响应参数", JSON.toJSON(responseParameter));
        return responseParameter;
    }

}
